package com.hotel_management_backstage.lin.service;

import com.hotel_management_backstage.lin.dao.SysRoleMenuMapper;
import com.hotel_management_backstage.lin.entity.SysRoleMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleMenuService {
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    public List<SysRoleMenu> selectAllByRid(int rid){
        return sysRoleMenuMapper.selectAllByRid(rid);
    }

    public List<SysRoleMenu> selectAllByRid(List<Integer> rids){
        return sysRoleMenuMapper.selectAllByManyRid(rids);
    }

    public void save(SysRoleMenu srm) {
        sysRoleMenuMapper.save(srm);
    }

    public void updateRoleMenu(int rid, Map<String, List<Integer>> menusIds){
        sysRoleMenuMapper.deleteAllByRid(rid);
        List<SysRoleMenu> srm = new ArrayList<>();
        for (Integer mid : menusIds.get("menusIds")){
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(mid);
            sysRoleMenu.setRoleId(rid);
            srm.add(sysRoleMenu);
        }
        sysRoleMenuMapper.saveAll(srm);
    }
}
